﻿using System.Windows.Forms;
using DiveDeeper.VsxLibrary.Core;
using Microsoft.VisualStudio.Shell;

namespace DiveDeeper.VsxLibrary.Core.ToolWindow
{
  // ==================================================================================
  /// <summary>
  /// This class is an extension of the <see cref="ToolWindowPane"/> class. Its goal is
  /// to make tool window definition even easier than using the 
  /// <see cref="ToolWindowPane"/> base class.
  /// </summary>
  /// <typeparam name="TPackage">Package owning the tool window.</typeparam>
  /// <typeparam name="TUIControl">
  /// Control implementing the UI of the tool window.
  /// </typeparam>
  /// <remarks>
  /// The derived class should be decorated with the following attributes to set up 
  /// the visual elements of the tool window: <see cref="InitialCaptionAttribute"/>, 
  /// <see cref="BitmapResourceIdAttribute"/>.
  /// </remarks>
  // ==================================================================================
  public abstract class VsxToolWindowPane<TPackage, TUIControl> : ToolWindowPane 
    where TPackage: Package
    where TUIControl: UserControl, new()
  {
    #region Private fields

    private readonly TUIControl _UIControl;

    #endregion

    #region Lifecycle methods

    // --------------------------------------------------------------------------------
    /// <summary>
    /// Constructs a new instance of a tool window and automatically creates its
    /// UI instance.
    /// </summary>
    // --------------------------------------------------------------------------------
    protected VsxToolWindowPane()
      : base(null)
    {
      // --- Obtain attributes of the class
      foreach (object attr in GetType().GetCustomAttributes(false))
      {
        InitialCaptionAttribute captionAttr = attr as InitialCaptionAttribute;
        if (captionAttr != null)
        {
          Caption = StringResolver<TPackage>.Resolve(captionAttr.Value);
          continue;
        }
        BitmapResourceIdAttribute resIdAttr = attr as BitmapResourceIdAttribute;
        if (resIdAttr != null)
        {
          BitmapResourceID = resIdAttr.ResourceId;
          BitmapIndex = resIdAttr.BitmapIndex;
          continue;
        }
      }

      // --- Create the user control 
      _UIControl = new TUIControl();
    }

    #endregion

    #region Public properties

    // --------------------------------------------------------------------------------
    /// <summary>
    /// Gets the UI instance of this tool window.
    /// </summary>
    // --------------------------------------------------------------------------------
    public TUIControl UIControl
    {
      get { return _UIControl; }
    }

    #endregion

    #region Overridden properties

    // --------------------------------------------------------------------------------
    /// <summary>
    /// Gets the window handle for the UI of this tool window pane.
    /// </summary>
    // --------------------------------------------------------------------------------
    public sealed override IWin32Window Window
    {
      get { return _UIControl; }
    }

    #endregion
  }
}
